package com.os.meituan.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;

import java.util.*;


/**
 * Created by xxx on 2017-7-26.
 */
public class XJsonInfo<T> {
    private Long total;
    private Integer totalPages;
    private Boolean success;
    private Integer code;
    private String msg;
    private T data;

    /**
     * Instantiates a new X json info.
     */
    public XJsonInfo() {
        this(true);
    }

    /**
     * Instantiates a new X json info.
     *
     * @param p_success the p success
     */
    public XJsonInfo(boolean p_success) {
        this.success = p_success;
        init();
    }

    /**
     * Instantiates a new X json info.
     *
     * @param p_success the p success
     * @param p_msg     the p msg
     */
    public XJsonInfo(boolean p_success, String p_msg) {
        this.success = p_success;
        this.msg = p_msg;
        init();
    }

    private void init() {

        if (this.success) {
            this.code = 0;
            if (this.msg == null) {
                this.msg = "操作成功";
            }
        } else {
            this.code = 10006;
            if (this.msg == null) {
                this.msg = "操作失败";
            }
        }

    }

    public Long getTotal() {
        return total;
    }

    public XJsonInfo setTotal(Long total) {
        this.total = total;
        return this;
    }

    public Integer getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(Integer totalPages) {
        this.totalPages = totalPages;
    }

    public Boolean getSuccess() {
        return success;
    }

    public XJsonInfo setSuccess(Boolean success) {
        this.success = success;
        return this;
    }


    public Boolean isSuccess() {
        return this.success;
    }

    @JsonIgnore
    public Boolean isNotSuccess() {
        return !success;
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    public static XJsonInfo success() {
        return new XJsonInfo(true);
    }

    public static XJsonInfo error() {
        return new XJsonInfo(false);
    }

    @JsonIgnore
    public XJsonInfo setPageData(PageInfo page) {
        this.totalPages = page.getPages();
        this.total = page.getTotal();
        this.data = (T) page.getList();
        return this;
    }

    @JsonIgnore
    public XJsonInfo setPageData(Page page) {
        this.totalPages = page.getTotalPages();
        this.total = page.getTotalElements();
        this.data = (T) page.getContent();
        return this;
    }


    /**
     * 设置返回的正文数据
     *
     * @param p_obj 正文内容
     */
    public XJsonInfo setData(T p_obj) {
        if (p_obj == null) {
            return this;
        }

        this.data = p_obj;

        if (p_obj.getClass().isArray()) {
            T[] os = (T[]) p_obj;
            this.total = (long) os.length;
        } else if (p_obj instanceof ArrayList) {
            ArrayList os = (ArrayList) p_obj;
            this.total = (long) os.size();
        } else {
            this.total = 1L;
        }

        return this;
    }

    /**
     * 设置返回的正文数据，一般分页会用到
     *
     * @param p_obj   正文内容
     * @param p_total 数据总行数
     */
    public XJsonInfo setData(T p_obj, Long p_total) {
        setData(p_obj);
        this.total = p_total;
        return this;
    }

    public XJsonInfo setData(T p_obj, Long p_total, int p_totalPages) {
        setData(p_obj);
        this.total = p_total;
        this.totalPages = p_totalPages;
        return this;
    }

    public XJsonInfo setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public XJsonInfo setCode(Integer code) {
        this.code = code;
        return this;
    }

//    /**
//     * 设置json文本
//     *
//     * @param raw 文本
//     */
//    public XJsonInfo setJsonData(String raw) throws IOException {
//        setData(JsonClass.fromJSON(raw, Map.class));
//        this.total = 1L;
//        return this;
//    }

    /**
     * 转为JSON格式
     *
     * @return JSON化后的实体
     */


//    /**
//     * 正文转为实体
//     */
//    public <T> T toDataModel(Class<T> modelclass) {
//        //return JsonClass.fromJSON(this.attr.toString(), modelclass);
//        return (T) this.data;
//    }

    /**
     * 正文转为实体集合
     */
    public List<T> toDataModelList() {
        return (List<T>) this.data;
    }

    public String toDataString() {
        try {
            return this.data.toString();
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 正文转为字典
     * REVIEW 这里要注意，如果data直接就是对象，这样tostring会出问题
     *
     * @return the t
     */


    public <T, S> XJsonInfo<T> reSetDataForPageData(T newData, XJsonInfo<S> source) {
        var re = new XJsonInfo<T>();
        re.setData(newData);
        re.setTotalPages(source.getTotalPages());
        re.setTotal(source.getTotal());
        re.setSuccess(source.getSuccess());
        re.setMsg(source.getMsg());
        re.setCode(source.getCode());

        return re;
    }

//    /**
//     * 正文转为集合
//     * 只针对data是string的类型
//     * @return the list
//     */
//    public List<String> toDataList() {
//        return JsonClass.fromJSONToListString(this.data.toString());
//    }

}
